/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pa165.languageschoolservice.serviceImpl;

import com.pa165.languageschoolpersistence.DAO.LevelOfLanguageDAO;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolpersistence.entities.Lector;
import com.pa165.languageschoolpersistence.entities.LevelofLanguage;
import com.pa165.languageschoolservice.DTO.LectorDTO;
import com.pa165.languageschoolservice.DTO.LevelofLanguageDTO;
import com.pa165.languageschoolservice.service.LevelOfLanguageService;
import java.util.ArrayList;
import java.util.List;



import javax.annotation.Resource;
import javax.validation.Valid;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Elena Medvedeva
 */
@Service("levelOfLanguageService")
public class LevelOfLanguageServiceImpl implements LevelOfLanguageService {

    @Resource(name = "levelOfLanguageDAO")
    private LevelOfLanguageDAO levelOfLanguageDAO;
    @Autowired
    private Mapper mapper;

    public void setLevelOfLanguageDAO(LevelOfLanguageDAO levelOfLanguageDAO) {
        this.levelOfLanguageDAO = levelOfLanguageDAO;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    public Mapper getMapper() {
        return mapper;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public LevelofLanguageDTO add(@Valid LevelofLanguageDTO levofLangDTO) throws ServiceFailureException{
        if (levofLangDTO == null) {
            throw new IllegalArgumentException("levofLangDTO is null in LevelOfLanguageServiceImpl.add ");
        }
        LevelofLanguage levelOfLang = mapper.map(levofLangDTO, LevelofLanguage.class);
        Lector lector = mapper.map(levofLangDTO.getLector(), Lector.class);


        lector.addLevelofLanguage(levelOfLang);

        LevelofLanguage res = levelOfLanguageDAO.add(levelOfLang);
        LevelofLanguageDTO result = mapper.map(res, LevelofLanguageDTO.class);

        return result;
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    @Transactional(readOnly = true)
    public boolean check(LevelofLanguageDTO levofLangDTO) throws ServiceFailureException {
        if (levofLangDTO == null) {
            throw new IllegalArgumentException("levofLangDTO is null in LevelOfLanguageServiceImpl.check ");
        }
        LevelofLanguage levelOfLang = mapper.map(levofLangDTO, LevelofLanguage.class);
        Lector lector = mapper.map(levofLangDTO.getLector(), Lector.class);

        List<LevelofLanguage> list = levelOfLanguageDAO.get(lector);
        boolean res = true;
        for (LevelofLanguage item : list) {
            if (item.getLanguageSpoken().equals(levelOfLang.getLanguageSpoken())) {
                res = false;
            }
        }
        return res;

    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void remove(@Valid LevelofLanguageDTO levofLangDTO) throws ServiceFailureException   {
        if (levofLangDTO == null) {
            throw new IllegalArgumentException("levofLangDTO is null in LevelOfLanguageServiceImpl.remove ");
        }
        if (levofLangDTO.getId() == null) {
            throw new IllegalArgumentException("levofLangDTO.Id is null in LevelOfLanguageServiceImpl.remove ");
        }
        LevelofLanguage levofLang = mapper.map(levofLangDTO, LevelofLanguage.class);
        levelOfLanguageDAO.remove(levofLang);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public LevelofLanguageDTO modify(@Valid LevelofLanguageDTO levofLangDTO)throws ServiceFailureException   {
        if (levofLangDTO == null) {
            throw new IllegalArgumentException("levofLangDTO is null in LevelOfLanguageServiceImpl.modify ");
        }
        if (levofLangDTO.getId() == null) {
            throw new IllegalArgumentException("levofLangDTO.Id is null in LevelOfLanguageServiceImpl.modify ");
        }
        LevelofLanguage levelOfLang = mapper.map(levofLangDTO, LevelofLanguage.class);
        LevelofLanguage res = levelOfLanguageDAO.modify(levelOfLang);
        return mapper.map(res, LevelofLanguageDTO.class);
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    @Transactional(readOnly = true)
    public LevelofLanguageDTO get(Long id)throws ServiceFailureException {
        if (id == null) {
            throw new IllegalArgumentException("Id is null LevelOfLanguageServiceImpl.get");
        }
        LevelofLanguage levofLang = levelOfLanguageDAO.get(id);
        LevelofLanguageDTO levofLangDTO = null;
        if (levofLang != null) {
            levofLangDTO = mapper.map(levofLang, LevelofLanguageDTO.class);
        }
        return levofLangDTO;
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    @Transactional(readOnly = true)
    public List<LevelofLanguageDTO> getByLector(@Valid LectorDTO lectorDTO)throws ServiceFailureException  {
        if ((lectorDTO == null) || (lectorDTO.getId() == null)) {
            throw new IllegalArgumentException("lectorDTO or it's Id is null LevelOfLanguageServiceImpl.get by lectorDTO ");
        }
        Lector lector = mapper.map(lectorDTO, Lector.class);
        List<LevelofLanguage> leveloflangs = levelOfLanguageDAO.get(lector);
        List<LevelofLanguageDTO> result = new ArrayList<LevelofLanguageDTO>();
        for (LevelofLanguage leveloflang : leveloflangs) {
            result.add(mapper.map(leveloflang, LevelofLanguageDTO.class));
        }
        return result;
    }
}
